<?php

namespace common\modules\ad\models\backend;

use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
 * ItemSearch represents the model behind the search form about `common\modules\ad\models\backend\Item`.
 */
class ItemSearch extends Item
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id',
                'position_id',
                'media_type',
                'start_time',
                'end_time',
                'click_count',
                'status',
                'created_at',
                'updated_at',
                'created_by',
                'updated_by'],
                'integer'],
            [['name', 'link', 'code', 'link_man', 'link_email', 'link_phone'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/ad', 'Item');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Item::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'          => $this->id,
            'position_id' => $this->position_id,
            'media_type'  => $this->media_type,
            'start_time'  => $this->start_time,
            'end_time'    => $this->end_time,
            'click_count' => $this->click_count,
            'status'      => $this->status,
            'created_at'  => $this->created_at,
            'updated_at'  => $this->updated_at,
            'created_by'  => $this->created_by,
            'updated_by'  => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'link_man', $this->link_man])
            ->andFilterWhere(['like', 'link_email', $this->link_email])
            ->andFilterWhere(['like', 'link_phone', $this->link_phone]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [

            [
                'attribute' => 'position_id',
                'value' => 'position.name',
                'vAlign' => 'middle',
                'width' => '220px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => ArrayHelper::map(Position::find()->all(), 'id', 'name'),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择广告位'],
                'format' => 'raw',
            ],

            [
                'attribute' => 'media_type',
                'value' => 'mediaType',
                'vAlign' => 'middle',
                'width' => '120px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => static::getMediaArray(),
                'filterInputOptions' => ['placeholder' => '选择类型'],
                'format' => 'raw',
            ],

            'name',

            [
                'attribute'  => 'start_time',
                'format'     => ['date', 'php:Y-m-d H:i:s'],
                'filter' => false,
            ],

            [
                'attribute'  => 'end_time',
                'format'     => ['date', 'php:Y-m-d H:i:s'],
                'filter' => false,
            ],

            'click_count',

            [
                'attribute' => 'status',
                'value' => function($model) {
                    /** @var static $model */
                    if ($model->status = self::STATUS_ON)
                    {
                        return '启用中';
                    }
                    elseif ($model->status = self::STATUS_OFF)
                    {
                        return '已停用';
                    }
                    return '未知';
                },
                'format' => 'html',
                'width' => '120px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => self::statusArray(),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '状态'],
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
